package com.yingxin.yxerp.module.permission.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yxerp.module.permission.entity.Permission;
import com.yingxin.yxerp.module.permission.entity.RolePermission;
import com.yingxin.yxerp.module.permission.mapper.PermissionMapper;
import com.yingxin.yxerp.module.permission.mapper.RolePermissionMapper;
import com.yingxin.yxerp.module.permission.service.IPermissionService;
import com.yingxin.yxerp.module.permission.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 角色-->权限 服务实现类
 * </p>
 *
 * @author fanchao
 * @since 2021-05-10
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    @SuppressWarnings("all")
    private PermissionMapper permissionMapper;

    /**
     * 根据 roleId 查询
     *
     * @param roleId
     * @return
     */
    @Override
    public List<RolePermission> listByRoleId(String roleId) {
        QueryWrapper<RolePermission> qw = new QueryWrapper<>();
        qw.lambda().eq(RolePermission::getRoleId, roleId);
        return list(qw);
    }


    public List<RolePermission> listByPermissionId(String permissionId) {
        QueryWrapper<RolePermission> qw = new QueryWrapper<>();
        qw.lambda().eq(RolePermission::getPermissionId, permissionId);
        return list(qw);
    }

    /**
     * 根据 roleIds 查询
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<RolePermission> listByRoleIds(Collection<String> roleIds) {
        if(CollectionUtil.isEmpty(roleIds)){
            return new ArrayList<>();
        }
        QueryWrapper<RolePermission> qw = new QueryWrapper<>();
        qw.lambda().in(RolePermission::getRoleId, roleIds);
        return list(qw);
    }

    /**
     * 根据角色id删除关系
     *
     * @param roleId
     */
    @Override
    public Boolean deleteByRoleId(String roleId) {
        UpdateWrapper<RolePermission> uw = new UpdateWrapper<>();
        uw.lambda().eq(RolePermission::getRoleId, roleId);
        return remove(uw);
    }


    public void rebuildPermissionId() {
        rebuildNewId("0", permissionService.listByParentId("0"));
    }

    public void rebuildNewId(String newParentId, List<Permission> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        int index = 1;
        for (Permission p : list) {
            String oldId = p.getId();
            String oldParentId = p.getParentId();
            List<Permission> childrenList = permissionService.listByParentId(oldId);
            String newId;
            if (oldParentId.equals("0")) {
                newId = "" + index;
            } else {
                if (index <= 9) {
                    newId = newParentId + "0" + index;
                } else {
                    newId = newParentId + index;
                }
            }
            rebuildNewId(newId, childrenList);
            p.setId(newId);
            p.setParentId(newParentId);
            index++;
            UpdateWrapper<RolePermission> uw = new UpdateWrapper<>();
            uw.lambda().set(RolePermission::getPermissionId, newId);
            uw.lambda().eq(RolePermission::getPermissionId, oldId);
            update(uw);
        }
        permissionMapper.saveTempPermissions(list);
    }

}
